/**
 * 
 */
package com.idea.zd.sys.interceptor;

import com.idea.zd.sys.constant.Const;
import com.idea.zd.sys.model.Admin;
import com.idea.zd.sys.util.RetDTO;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

/**
 * @author y.z
 */
public class AdminInterceptor implements Interceptor {

	/* (non-Javadoc)
	 * @see com.jfinal.aop.Interceptor#intercept(com.jfinal.aop.Invocation)
	 */
	@Override
	public void intercept(Invocation inv) {
		Controller controller = inv.getController();
		//String serverURL = controller.getRequest().getServletPath();
		
		Admin admin = controller.getSessionAttr("admin");
		
		if(null == admin){
			String ajax = controller.getRequest().getHeader("X-Requested-With");

			if("XMLHttpRequest".equals(ajax)){
				RetDTO ret = new RetDTO();
				ret.setCode(Const.SESSIONOUT_ERROR_CODE);
				ret.setMsg(Const.SESSIONOUT_ERROR_MSG);
				controller.renderJson(ret);
				return;
			}
			
			controller.redirect("/");
			return;
		}
		
		inv.invoke();
	}

}
